package oocl.eshop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oocl.eshop.dao.MerchantDao;
import oocl.eshop.po.Merchant;
import oocl.eshop.util.dbutil.DbUtil;

public class MerchantDaoImpl implements MerchantDao {

	public List<Merchant> findAllMerchants() {
		List<Merchant> ms = new ArrayList<Merchant>();

		String sql = "select * from merchant";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				int mId = rs.getInt("mId");
				String mPersonName = rs.getString("mPersonName");
				String mTel = rs.getString("mTel");
				String mIdCard = rs.getString("mIdCard");
				String mCardPath = rs.getString("mCardPath");
				String mLocation = rs.getString("mLocation");
				String mBrand = rs.getString("mBrand");
				String mLogoPath = rs.getString("mLogoPath");
				String mPassword = rs.getString("mPassword");

				Merchant merchant = new Merchant(mPersonName, mTel, mPassword, mIdCard, mCardPath, mLocation, mBrand,
						mLogoPath);
				merchant.setmId(mId);
				ms.add(merchant);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return ms;
	}

	public int addMerchant(Merchant m) {
		String sql = "insert into MERCHANT (mid, mPersonName, mTel, mIdCard, mCardPath, mLocation, mBrand, mLogoPath, mPassword)values (elema_sql01.nextval, ?,?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		int n = 0;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, m.getmPersonName());
			pst.setString(2, m.getmTel());
			pst.setString(3, m.getmIdCard());
			pst.setString(4, m.getmCardPath());
			pst.setString(5, m.getmLocation());
			pst.setString(6, m.getmBrand());
			pst.setString(7, m.getmLogoPath());
			pst.setString(8, m.getmPassword());
			n = pst.executeUpdate();
		} catch (SQLException e) {
			System.out.println("添加异常");
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}

		return n;
	}

	public int deleteMerchant(int mId) {
		String sql = "delete from MERCHANT where mid = ?";
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, mId);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			System.out.println("删除异常");
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	public int updateMerchant(Merchant m) {
		String sql = "update merchant set mLocation=?,mBrand=?,mLogoPath=?,mPassword=? where mid=?";
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		int n = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setString(1, m.getmLocation());
			pst.setString(2, m.getmBrand());
			pst.setString(3, m.getmLogoPath());
			pst.setString(4, m.getmPassword());
			pst.setInt(5, m.getmId());
			n = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}

		return n;
	}

	public Merchant findMerchantByTel(String mTel) {
		Merchant merchant = null;

		String sql = "select * from merchant where mTel=? ";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		System.out.println(con);
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, mTel);
			rs = pst.executeQuery();
			if (rs.next()) {
				int mId = rs.getInt("mId");
				String mPersonName = rs.getString("mPersonName");
				String mIdCard = rs.getString("mIdCard");
				String mCardPath = rs.getString("mCardPath");
				String mLocation = rs.getString("mLocation");
				String mBrand = rs.getString("mBrand");
				String mLogoPath = rs.getString("mLogoPath");
				String mPassword = rs.getString("mPassword");

				merchant = new Merchant(mPersonName, mTel, mPassword, mIdCard, mCardPath, mLocation, mBrand, mLogoPath);
				merchant.setmId(mId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return merchant;
	}

	public Merchant findMerchantById(int mId) {
		Merchant merchant = null;

		String sql = "select * from merchant where mId=? ";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, mId);
			rs = pst.executeQuery();
			if (rs.next()) {
				String mPersonName = rs.getString("mPersonName");
				String mTel = rs.getString("mTel");
				String mIdCard = rs.getString("mIdCard");
				String mCardPath = rs.getString("mCardPath");
				String mLocation = rs.getString("mLocation");
				String mBrand = rs.getString("mBrand");
				String mLogoPath = rs.getString("mLogoPath");
				String mPassword = rs.getString("mPassword");

				merchant = new Merchant(mPersonName, mTel, mPassword, mIdCard, mCardPath, mLocation, mBrand, mLogoPath);
				merchant.setmId(mId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return merchant;
	}

}
